package com.zl.order.service.impl;

import com.zl.basic.exception.BusinessException;
import com.zl.basic.service.IBaiduAiAuditService;
import com.zl.basic.service.impl.BaseServiceImpl;
import com.zl.order.domain.Product;
import com.zl.order.domain.ProductDetail;
import com.zl.order.mapper.ProductDetailMapper;
import com.zl.order.mapper.ProductMapper;
import com.zl.order.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void save(Product product) {

        super.save(product);
        ProductDetail productDetail1 = product.getProductDetail();
        if (productDetail1!=null){
            productDetail1.setProductId(product.getId());
            productDetailMapper.save(productDetail1);
        }

    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();

        if (productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.updatePetDetailByPetId(productDetail);

        }

    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        productDetailMapper.delProductDetailByPetId(id);
    }


    @Override
    public void onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
            Boolean imgBoo = baiduAiAuditService.imageAudit(product.getResources());
            if (textBoo&&imgBoo){
                product.setOnsaletime(new Date());
                product.setState(1L);
                productMapper.update(product);

            }else {
                throw new BusinessException("上架失败！！！");
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        Date offdate = new Date();
        productMapper.offsale(ids,offdate);
    }
}
